<?php

namespace App\Http\Controllers\Admin;

use App\Models\Menu;
use App\Models\RoleMenu;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class UserController extends Controller
{


    /**
     * 列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {


        return view('admin/user/index');
    }

    /**
     * 创建页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        $menu = Menu::where('p_id', 0)->get();
        return view('admin/user/add', ['menu' => $menu]);
    }

    /**
     * 编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $params = $request->only(['user_id']);
        $validator = \Validator::make($params, [
            'user_id' => 'required|exists:users,id',
        ], [
            'user_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user = User::find($request->user_id);
        return view('admin/user/edit', [ 'data' => $user]);
    }

    /**
     * 用户管理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {

        $searchParams = json_decode($request->searchParams, true);
        $phone = $searchParams['phone'] ?? '';
        $name = $searchParams['name']??'';
        $sort = $searchParams['sort']??'';
        $data = User::from('users')
            ->Phone($phone)
            ->Name($name)
            ->leftJoin('orders', 'orders.user_id', '=', 'users.id')
            ->select('users.id as user_id','users.is_buyers','users.is_seller', 'users.name','users.status', 'users.phone', \DB::raw('count(hg_orders.id) as order_count , sum(hg_orders.price) as order_price'))
            ->Sort($sort)
            ->with('buyers','seller')
            ->groupBy('users.id', 'users.name', 'users.phone')
            ->paginate($request->limit)->toArray();
        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
//        return $this->success($data);
    }

    /**
     * 用户信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(Request $request)
    {
        $params = $request->only(['user_id']);
        $validator = \Validator::make($params, [
            'user_id' => 'required|exists:users,id',
        ], [
            'user_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user = User::find($request->user_id);

        return $this->success($user);

    }

    /**
     * 用户编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveUser(Request $request)
    {
        $params = $request->only(['name', 'head', 'user_id']);
        $validator = \Validator::make($params, [
            'user_id' => 'required|exists:users,id',
            'name' => 'required|string|max:30',
            'head' => 'required',
        ], [
            'user_id.*' => '参数错误',
            'name.*' => '昵称填写错误',
            'head.*' => '头像不能为空',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user_id = $request->user_id;
        $name = $request->name;
        $head = $request->head;
        $re = User::saveUser($user_id, $name, $head);
        if ($re) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->success(['msg' => '修改失败']);
    }

    /**
     * 用户冻结/解冻
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function operationUser(Request $request)
    {

        $params = $request->only(['user_id', 'status']);
        $validator = \Validator::make($params, [
            'user_id' => 'required|exists:users,id',
//            'status' => ['required', Rule::in([1, 2])]
        ], [
            'user_id.*' => '用户参数错误',
//            'status.*' => '状态参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user_id = $request->user_id;
//        $status = $request->status;

        $re = User::freeze($user_id);
        if ($re) {
            return $this->success(['msg' => '操作成功']);
        }
        return $this->success(['msg' => '操作失败']);
    }

    /**
     *用户路由权限
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function userRoleMenu(Request $request)
    {
        $admin = \Auth::user();
        //如果是超级管理员
        if ($admin->id == 1) {
            $data = Menu::get();
        } else {
            $role_ids = RoleMenu::where('role_id', $admin->role_id)->pluck('menu_id')->toArray();
            $data = Menu::whereIn('id', $role_ids)->get();
        }
        return $this->success($data);

    }

    public function userMenus($role_ids = [])
    {
        if (count($role_ids) > 0) {

            $data = Menu::get();
        }


    }
}
